#include<bits/stdc++.h>
using namespace std;
const int N=15;

struct Node
{
    int state;
    vector<int> b;
    Node(){}
    Node(int state_,vector<int> b_):state{state_},b{b_}
    {}
};

int arr[N][5];
int ans=17;
unordered_map<int,vector<int> > mp;
unordered_map<int,vector<int> > mp2;


int main()
{
    int state,state2;

    for(int i=1;i<=12;i++)
    {
        int temp;
        scanf("%d",&temp);
        state=state<<2;
        state+=temp-1;
        for(int j=0;j<4;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }

    if(state==0)
    {
        printf("0");
        return 0;
    }
    Node node,node2;
    node.state=state;
    node2.state=0;

    queue<Node> que;
    queue<Node> que2;
    que.push(node);
    que2.push(node2);

    mp[state]={};
    mp2[0]={};

    int step=0,step2=0;
    while(!que.empty()&&!que2.empty())
    {
    	//begin->end
        step++;
        int size=que.size();
        mp.clear();
        for(int k=0;k<size;k++)
        {
            node=que.front();
            que.pop();
            int state=node.state;
            vector<int> b=node.b;
            int sum=0;
            while(state!=0)
            {
                sum+=state%4;
                state/=4;
            }

            if(step-1+sum/2+sum%2+step2>ans) continue;
            
            Node node1;
            int pre,in,post;
            for(int i=1;i<=12;i++)
            {
                state=node.state;
                pre=state>>(26-2*i);
                post=state%(1<<(24-2*i) );
                in=(state>>(24-2*i) )%4;

                int j=arr[i][in];
                in=(in+1)%4;
                state=node1.state=(pre<<(26-2*i) )+( in<<(24-2*i) )+post;

                if(i!=j)
                {
                	pre=state>>(26-2*j);
	                post=state%(1<<(24-2*j) );
	                in=(state>>(24-2*j) )%4;
	                in=(in+1)%4;
	                state=node1.state=(pre<<(26-2*j) )+(in<<(24-2*j) )+post;
				}
                node1.b=b;
                node1.b.emplace_back(i);
                if(mp2.count(state) )
                {
                    printf("%d\n",step+step2);
                    for(auto u:node1.b)
                    {
                        printf("%d ",u);
                    }
                    vector<int> b2=mp2[state];
                    for(int i=b2.size()-1;i>=0;i--)
                    {
                        printf("%d ",b2[i]);
                    }
                    return 0;
                }
                if(mp.count(state)) continue;
                mp[state]=node1.b;
                que.push(node1);
            }
        }
        
        //end->begin
        step2++;
        mp2.clear();
        size=que2.size();
        for(int k=0;k<size;k++)
        {
            node=que2.front();
            que2.pop();
            int state=node.state;
            vector<int> b=node.b;
            int sum=0;
            while(state!=0)
            {
                sum+=state%4;
                state/=4;
            }

            if(step-1+sum/2+sum%2+step2>ans) continue;
            
            Node node1;
            int pre,in,post;
            for(int i=1;i<=12;i++)
            {
                state=node.state;
                pre=state>>(26-2*i);
                post=state%(1<<(24-2*i) );
                in=(state>>(24-2*i) )%4;
                if(in==0) in=3;
                else in--;
                int j=arr[i][in];

                state=node1.state=(pre<<(26-2*i) )+(in<<(24-2*i))+post;
                
                if(i!=j)
                {
                	pre=state>>(26-2*j);
	                post=state%(1<<(24-2*j) );
	                in=(state>>(24-2*j) )%4;
	                if(in==0) in=3;
	                else in--;
	                state=node1.state=(pre<<(26-2*j) )+(in<<(24-2*j) )+post;
				}
                node1.b=b;
                node1.b.emplace_back(i);
                if(mp.count(state) )
                {
                    printf("%d\n",step+step2);
                    vector<int> b2=mp[state];
                    for(auto u:b2)
                    {
                        printf("%d ",u);
                    }
                    for(int i=node1.b.size()-1;i>=0;i--)
                    {
                        printf("%d ",(node1.b)[i]);
                    }
                    return 0;
                }
                if(mp2.count(state)) continue;
                mp2[state]=node1.b;
                que2.push(node1);
            }
        }
    }
    printf("-1");
    return 0;
}
